package cn.com.sueframe.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 缓存时间设置拦截器
 * @author anmin
 * Create 2013-3-21
 */
public class CacheFilter implements Filter {
	//缓存时长
    private String cacheTime = "0";
	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
       HttpServletRequest httpRequest = (HttpServletRequest)request;
	   HttpServletResponse httpResponse = (HttpServletResponse)response;
	   httpResponse.setHeader("Cache-Control", "private,max-age=" + cacheTime);
	   chain.doFilter(httpRequest, httpResponse);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		cacheTime = filterConfig.getInitParameter("cacheTime");
		cacheTime = cacheTime == null || "".equals(cacheTime) ? "0" : cacheTime;
	}

}
